package com.ssm.wzl.test;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by Administrator on 2017/5/18 0018.
 */
public class GeneralProxyDemo {

    static interface IServiceA{
        void sayHello();
    }

    static class ServiceImpl implements IServiceA{

        @Override
        public void sayHello() {
            System.out.println("Hello!!!");
        }
    }


    static class SimpleInvocationHandler implements InvocationHandler{

        private Object mObject;
        public SimpleInvocationHandler(Object mObject){
            this.mObject = mObject;
        }


        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("entering"+method.getName());
            Object result = method.invoke(mObject, args);
            System.out.println("leaving"+method.getName());
            return result;
        }
    }



    public static void main(String [] arg){
        IServiceA serviceA = new ServiceImpl();
        IServiceA iServiceA = (IServiceA) Proxy.newProxyInstance(IServiceA.class.getClassLoader(),new Class<?>[]{IServiceA.class},new SimpleInvocationHandler(serviceA));
        iServiceA.sayHello();
    }








}
